package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import com.exartisan.adapter.EventFeedAdapter;
import com.exartisan.bean.Event;
import com.exartisan.bean.FbComment;
import com.exartisan.network.FacebookResponseParser;
import com.exartisan.network.FbConnnector;
import com.exartisan.network.NetworkUtility;
import com.exartisan.util.FbUtil;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Session.StatusCallback;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.image.SmartImageView;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EventFeedActivity extends BaseActivity {

	private ListView listViewFeed;
	private TextView tvName;
	private TextView tvDesc;
	private TextView tvStartDay;
	private TextView tvEndDay;
	private SmartImageView imgCover;
	private EventFeedAdapter feedAdapter;
	private ArrayList<FbComment> comments;
	
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setCustomView(R.layout.ab_feed);
	}

	@Override
	protected void initUI() {
		setContentView(R.layout.activity_event_feed);
		listViewFeed = (ListView) findViewById(R.id.listView);
		//View header = getLayoutInflater().inflate(R.layout.header_feed, listViewFeed, false);

	}

	@Override
	protected void initProcess() {
		
		View header = getLayoutInflater().inflate(R.layout.header_feed, listViewFeed, false);
		listViewFeed.addHeaderView(header);
		feedAdapter = new EventFeedAdapter(this);
		listViewFeed.setAdapter(feedAdapter);
		tvName = (TextView) findViewById(R.id.tvName);
		tvDesc = (TextView) findViewById(R.id.tvDesc);
		tvStartDay = (TextView) findViewById(R.id.tvTimeStart);
		tvEndDay = (TextView) findViewById(R.id.tvTimeEnd);
		imgCover = (SmartImageView) findViewById(R.id.imgCover);
		tvName.setText(event.getEventName());
		tvDesc.setText(event.getDescription());
		tvStartDay.setText(event.getStartDate());
		tvEndDay.setText(event.getEndDate());
		imgCover.setImageUrl(event.getThumbUrl());
		getFbComments();
	}
	
	public void onClickBack(View v){
		finish();
	}
	
	public void onClickShare(View v){
		
		Session session = Session.getActiveSession();
		if(session==null||session.isClosed()) Session.openActiveSession(getActivity(), true, new StatusCallback() {
			
			@Override
			public void call(Session session, SessionState state, Exception exception) {
				if(session != null){
					if(state.isOpened()){
						FbUtil.ShareToFb(getActivity(), event.getEventName(), event.getDescription(), FbUtil.YN_LINK, event.getThumbUrl());
					}
				}
			}
		}); else
			FbUtil.ShareToFb(getActivity(), event.getEventName(), event.getDescription(), FbUtil.YN_LINK, event.getThumbUrl());
		
	}
	
public void onClickRefresh(View v){
		getFbComments();
	}
	
	public void onClickDetail(View v){
		Intent i = new Intent(this, EventDetailActivity.class);
		startActivity(i);
	}
	
	public void onClickGallery(View v){
		Intent i = new Intent(this, GalleryActivity.class);
		startActivity(i);
	}
	
	public void onClickJoin(View v){
		PackageManager pm = getPackageManager();
		Intent i = pm.getLaunchIntentForPackage("com.exartisan.molisasurvey");
		startActivity(i);
	}
	
	private void getFbComments(){
		if(NetworkUtility.checkNetworkState(getApplicationContext())){
			//prBar.setVisibility(View.VISIBLE);
			//Log.d("feedID", event.getFbFeedID());
			RequestParams params = new RequestParams("ids", "453407711441834_458009987648273");
			FbConnnector.get(null, "/comments", params, new AsyncHttpResponseHandler(){
				
				@Override
				public void onSuccess(int arg0, String response) {
					//prBar.setVisibility(View.GONE);
					if (response == null){
						Toast.makeText(getApplicationContext(), "Không thể tải bình luận", Toast.LENGTH_SHORT).show();
						return;
					}
					Log.d("fbRes", response.toString());
					comments = FacebookResponseParser.getComments(response, "453407711441834_458009987648273");
					Log.d("comments_num", comments.size() + "");
					feedAdapter.setFbComments(comments);
					feedAdapter.notifyDataSetChanged();
					super.onSuccess(arg0, response);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					//prBar.setVisibility(View.GONE);
					Toast.makeText(getApplicationContext(), "Không thể tải bình luận", Toast.LENGTH_SHORT).show();
					super.onFailure(arg0, arg1);
				}
				
			});
		} else Toast.makeText(this, "Không có kết nối", Toast.LENGTH_SHORT).show();
	}
	
	private void comment(final String comment){
		Log.d("token", Session.getActiveSession().getAccessToken());
		
		FbConnnector.post(event.getFbFeedID(), "/comments?access_token=" + FbUtil.ACCESS_TOKEN, new RequestParams("message", comment), new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int arg0, String response) {
				Log.d("commentResponse", response.toString());
				getFbComments();
				super.onSuccess(arg0, response);
			}
			
			@Override
			public void onFailure(Throwable arg0, String arg1) {
				Toast.makeText(getActivity(), "Không thể đăng bình luận", Toast.LENGTH_SHORT).show();
				Log.d("commentResponse", "fail");
				super.onFailure(arg0, arg1);
			}
		});
	}
	

}
